package com.fpd.third.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fpd.model.bean.Logistics;
import com.fpd.model.bean.ResponseXmlBean;
import com.fpd.model.ro.PostBaseInfo;
import com.fpd.third.service.EMS;
import com.fpd.tools.ConvertUtil;
import com.fpd.tools.Tool;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @desc 测试环境实现类
 * @author wujiangbo
 * @date 2019年11月28日 下午1:58:35
 */
@Service
@Slf4j
public class EmsTest implements EMS {

	@Value("${ems.logisticsUrl}")
	private String logisticsUrl;

	@Value("${ems.version}")
	private String version;

	@Value("${ems.authenticate}")
	private String authenticate;

	@Override
	public ResponseXmlBean updatePrintDatas(PostBaseInfo post) {
		ResponseXmlBean response = new ResponseXmlBean();
		response.setResult("1");// 0：有错误；1：无错误
		return response;
	}

	public static void main(String[] args) {
		EmsTest et = new EmsTest();
		List<Logistics> list = et.getLogisticsInfo("1169599245530");
		for (int i = 0; i < list.size(); i++) {
			Logistics obj = list.get(i);
			System.out.println(obj.toString());
		}
	}

	@Override
	public List<Logistics> getLogisticsInfo(String billNo) {
		List<Logistics> list = new ArrayList<Logistics>();
		try {
//			String url = logisticsUrl + "1169599245530";
			String url = logisticsUrl + billNo;
			HttpRequest request = HttpUtil.createGet(url);
			// 设置验证信息
			Map<String, String> headers = new HashMap<String, String>();
			headers.put("version", version);
			headers.put("authenticate", authenticate);
			request.addHeaders(headers);
			long beginTIme = System.currentTimeMillis();
			HttpResponse response = request.execute();
			long endTIme = System.currentTimeMillis();
			log.info("根据快递单号查询物流信息---接口花费时长：{}秒", (endTIme - beginTIme) / 1000);
			// 获取状态码
			int status = response.getStatus();
			log.info("根据快递单号查询物流信息，状态码：{}", status);
			if (status == 200) {
				byte[] xmlString = response.bodyBytes();
				// 得到物流信息
				String res = new String(xmlString, "UTF-8");
				log.info("根据快递单号查询物流信息，物流信息：{}", res);
				// 解析物流信息
				JSONObject result_obj = JSONObject.parseObject(res);
				JSONArray jsonArray = result_obj.getJSONArray("traces");
				Logistics logistics = null;
				// 遍历即可，foreach或for循环
				for (Object value : jsonArray) {
					logistics = JSON.parseObject(value.toString(), Logistics.class);
					logistics.setCodeDesc(ConvertUtil.convertCode(logistics.getCode()));
					logistics.setCode2Desc(ConvertUtil.convertCode2(logistics.getCode2()));
					list.add(logistics);
				}
			}
		} catch (UnsupportedEncodingException e) {
			log.error("根据快递单号查询物流信息接口-------异常：{}", e);
		}
		return list;
	}

	// 测试单号：1169599245530
	@Override
	public String getBillNo() {
		return Tool.getPrimaryKey();
	}

}
